package website.sgg;

import cn.hutool.core.thread.ConcurrencyTester;
import cn.hutool.core.thread.ThreadUtil;

import java.io.IOException;

/**
 * 单例模式：双重校验锁
 *
 * @author: longhc@citycloud.com.cn
 * @since: 2023/1/10 9:57
 * @version: 1.0
 */
public class Singleton {
    private Singleton() {
    }

    private static volatile Singleton INSTANCE;

    public static synchronized Singleton getInstance() {
        if (INSTANCE == null) {
            synchronized (Singleton.class) {
                if (INSTANCE == null) {
                    INSTANCE = new Singleton();
                }
            }
        }
        return INSTANCE;
    }

    public static void main(String[] args) throws IOException {
        ConcurrencyTester concurrencyTester = ThreadUtil.concurrencyTest(10, () -> {
            Singleton instance = Singleton.getInstance();
            System.out.println(instance.hashCode());
        });
        concurrencyTester.close();
    }
}
